const Koa = require('koa');
const router = require('koa-router')();
const path = require('path');
const cors = require('koa-cors')
// const bodyParser =  require('koa-bodyparser');
// const koaBody = require('koa-body');
const multer = require('koa-multer')
const app = new Koa();
// app.use(bodyParser());
// app.use(koaBody({
//     multipart:true
// }))
app.use(cors())
// 持久化存储
let storage = multer.diskStorage({
    destination:path.resolve('upload'),
    filename:(ctx,file,cb) => {
        cb(null,file.originalname)
    }
});
// 上传文件 中间件
let upload = multer({
    storage
})

// 第二个参数，启用upload中间件，设置 /upload 路由时启动
router.post('/upload',upload.single('file'), async (ctx) => {
    console.log(ctx.request, '/////');
    ctx.body = ctx.request.body;
})
app.use(router.routes(), router.allowedMethods())
app.listen(8080);